﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;

namespace AlarmManagePlatform.UC
{
    public partial class ReportStatistics : UserControl
    {
        public ReportStatistics()
        {
            InitializeComponent();

            InitControls();

            btnSearch_Click(null,null);
        }

        public void InitControls()
        {
            Static.Methods.CreateDGVColumns(ref dgvReportInfo,Model.SystemEnum.DGVType.ReportStatisticsInfo);
            dgvReportInfo.Columns["Selected"].Visible = false;

            radDay.Checked = true;
            dtBegin.CustomFormat = "yyyy年MM月dd日";
            dtFinish.CustomFormat = "yyyy年MM月dd日";
            dtBegin.Value = DateTime.Now.AddDays(-1);
            dtFinish.Value = DateTime.Now;

            radDay.CheckedChanged += RadButton_CheckedChanged;
            radMonth.CheckedChanged += RadButton_CheckedChanged;

            pagReport.PageSize = 50;
            pagReport.Event_SearchDataInfo += PagReport_Event_SearchDataInfo;
            pagReport.Event_UpdatePagingControl += btnSearch_Click;

            InitComBoxUserInfo();
        }
        
        private void PagReport_Event_SearchDataInfo(int pageIndex, int pageSize)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new Action<int, int>(PagReport_Event_SearchDataInfo), new object[] { pageIndex, pageSize });
            }
            else
            {
                int _StartIndex = (pageIndex * pageSize);
                int _SearchCount = pageSize;

                Dictionary<string, string> _LogicList;

                _LogicList = GetLogicList();

                try
                {
                    List<RelationsLMP.Model.ReportInfo> _ReportInfoList = new DataAccess.tb_AlarminstrumentHisData().GetReportInfo(_StartIndex, _SearchCount, _LogicList);
                    dgvReportInfo.DataSource = _ReportInfoList;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(this, "统计信息失败，错误信息：" + ex.Message, "异常信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

        private void RadButton_CheckedChanged(object sender, EventArgs e)
        {
            if (radDay.Checked)
            {
                dtBegin.CustomFormat = "yyyy年MM月dd日";
                dtFinish.CustomFormat = "yyyy年MM月dd日";

                dtBegin.Value = DateTime.Now.AddDays(-1);
                dtFinish.Value = DateTime.Now;
            }
            else
            {
                dtBegin.CustomFormat = "yyyy年MM月";
                dtFinish.CustomFormat = "yyyy年MM月";

                dtBegin.Value = DateTime.Now.AddMonths(-1);
                dtFinish.Value = DateTime.Now;
            }
        }

        private void InitComBoxUserInfo()
        {
            cmbUser.DropDownStyle = ComboBoxStyle.DropDownList;
            cmbUser.DisplayMember = "userName";
            cmbUser.ValueMember = "userId";

            List<Model.DB.tb_User> _UserList = new List<Model.DB.tb_User>();
            _UserList = new DataAccess.tb_User().getAll().ToList();
            _UserList.Insert(0,new Model.DB.tb_User() { userName = "全部人员",userId = -1});

            cmbUser.DataSource = _UserList;

            if (cmbUser.Items.Count > 0)
                cmbUser.SelectedIndex = 0;

            //cmbUser.SelectedIndexChanged += CmbUser_SelectedIndexChanged;
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {

            Thread_SearchReportCount();

            //Thread _SearchReportCountTH = new Thread(new ThreadStart(Thread_SearchReportCount));
            //_SearchReportCountTH.IsBackground = true;
            //_SearchReportCountTH.Start();
        }

        void Thread_SearchReportCount()
        {

            int _DataCount;
            if (cmbUser.SelectedItem == null)
            {
                MessageBox.Show(this, "请选择检测人员信息！", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            RelationsLMP.Control.Form.WaitForm.ShowSplashScreen();
            RelationsLMP.Control.Form.WaitForm.ShowRuningMessage("正在统计，请稍等......");

            Dictionary<string, string> _LogicList;

            try
            {
                _LogicList = GetLogicList();

                _DataCount = new DataAccess.tb_AlarminstrumentHisData().GetInstrumentHisDataListCount(_LogicList);

                pagReport.IniEnabled(_DataCount);
                gbCountResult.Text = "统计结果/" + _DataCount;

            }
            catch (Exception ex)
            {
                MessageBox.Show(this, "统计信息失败，错误信息：" + ex.Message, "异常信息", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                RelationsLMP.Control.Form.WaitForm.Instance.BeginInvoke(new MethodInvoker(RelationsLMP.Control.Form.WaitForm.Instance.Dispose));
                RelationsLMP.Control.Form.WaitForm.Instance = null;
            }
        }

        private Dictionary<string, string> GetLogicList()
        {
            DateTime _BeginTime = dtBegin.Value, _EndTime = dtFinish.Value;
            Dictionary<string, string> _LogicList;

            int _UserId;

            _UserId = (int)cmbUser.SelectedValue;

            _LogicList = new Dictionary<string, string>();

            if (radDay.Checked)
            {
                _LogicList.Add("BeginTime", _BeginTime.ToString("yyyy/MM/dd 00:00:00"));
                _LogicList.Add("FinishTime", _EndTime.ToString("yyyy/MM/dd 23:59:59"));
            }
            else
            {
                _LogicList.Add("BeginTime", _BeginTime.ToString("yyyy/MM/01 00:00:00"));
                _LogicList.Add("FinishTime", DateTime.ParseExact(_EndTime.AddMonths(1).ToString("yyyy/MM/01"), "yyyy/MM/dd", Thread.CurrentThread.CurrentCulture).AddDays(-1).ToString("yyyy/MM/dd 23:59:59"));
            }
            _LogicList.Add("UserId", _UserId == -1 ? "" : _UserId.ToString());

            return _LogicList;
        }

        private void btnReport_Click(object sender, EventArgs e)
        {

        }
    }
}
